package com.exsun.gateway.jt809.common;

import io.netty.channel.ChannelId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

public class FlowIdMgr {

    private static Logger logger = LoggerFactory.getLogger(FlowIdMgr.class);

    // 链路的发送消息流水号
    private static ConcurrentHashMap<ChannelId, Integer> channelFlowId = new ConcurrentHashMap<ChannelId, Integer>();

    // 获取链路的流水号
    public static int getFlowId(ChannelId channelId) {
        if (channelFlowId.containsKey(channelId)) {
            Integer currentFlowId = channelFlowId.get(channelId);
            if (currentFlowId >= Integer.MAX_VALUE) {
                currentFlowId = 0;
            }
            channelFlowId.put(channelId, ++currentFlowId);
            return currentFlowId;
        } else {
            channelFlowId.put(channelId, 0);
        }
        return 0;
    }

    public static void removeChannelId(ChannelId channelId){
        if(channelFlowId.containsKey(channelId))channelFlowId.remove(channelId);
    }
}
